/*
 * TelaListarAgencia
 * 
 * Versao 1.0
 * 
 * 25/05/2013
 * 
 * Autor: Bhetynna Catalunha
 * 
 * 
 * 
 */
package edu.Ufes.exeSwing.Boundary;

import edu.Ufes.exeSwing.Entity.Agencia;
import edu.Ufes.exeSwing.Control.ControlAgencia;
import java.util.Iterator;
import java.util.TreeSet;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author bhetynnacatalunha
 */
public class TelaListarAgencia extends javax.swing.JFrame {

    /**
     * Creates new form TelaListarAgencia
     */
    private DefaultTableModel tm = null;

    public TelaListarAgencia() {
        initComponents();
        ControlAgencia c = new ControlAgencia();
        carregaAgencias(c.recuperarAgencias());

    }

    public void carregaAgencias(TreeSet agencias) {
            tm = new DefaultTableModel(new Object[][]{}, new String[]{"Codigo", "Cidade", "UF"}) {
            @Override
            public boolean isCellEditable(int row, int col) {
                return false;
            }
        };
        tm.setNumRows(0);
        Iterator<Agencia> it = agencias.iterator();
        while (it.hasNext()) {
            Agencia a = it.next();
            String linha = a.toString();
            String valores[] = linha.split(",");
            tm.addRow(valores);


        }
        tblAgencias.setModel(tm);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblAgencias = new javax.swing.JTable();
        btnNova = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        btnFechar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Agencias");

        tblAgencias.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Codigo", "Cidade", "UF"
            }
        ));
        jScrollPane1.setViewportView(tblAgencias);

        btnNova.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/Ufes/exeSwing/Imagem/imgNew.gif"))); // NOI18N
        btnNova.setText("Nova");
        btnNova.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovaActionPerformed(evt);
            }
        });

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/Ufes/exeSwing/Imagem/imgEdit.png"))); // NOI18N
        btnEditar.setText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        btnExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/Ufes/exeSwing/Imagem/imgDelete.gif"))); // NOI18N
        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        btnFechar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/Ufes/exeSwing/Imagem/imgCancel.gif"))); // NOI18N
        btnFechar.setText("Fechar");
        btnFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFecharActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap(101, Short.MAX_VALUE)
                .add(btnNova)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(btnEditar)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(btnExcluir)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(btnFechar)
                .addContainerGap())
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 285, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 7, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnNova)
                    .add(btnEditar)
                    .add(btnExcluir)
                    .add(btnFechar)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNovaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovaActionPerformed
        // TODO add your handling code here:
        InclusaoAgencia ia = new InclusaoAgencia(this, null);
        ia.setVisible(true);

    }//GEN-LAST:event_btnNovaActionPerformed

    private void btnFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFecharActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnFecharActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        // TODO add your handling code here:
        if (this.tblAgencias.getSelectedRow() != -1) {
            ControlAgencia c = new ControlAgencia();
            if (JOptionPane.showConfirmDialog(rootPane, "Deseja excluir o cadastro?") == 0) {
                if (c.removeAgencia((String) this.tblAgencias.getValueAt(this.tblAgencias.getSelectedRow(), 0))) {
                    this.tm.removeRow(this.tblAgencias.getSelectedRow());
                    JOptionPane.showMessageDialog(this, "Agencia Excluida com sucesso!");
                    this.carregaAgencias(c.recuperarAgencias());
                } else {
                    JOptionPane.showMessageDialog(this, "Erro ao excluir!");
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "Selecione uma linha");
        }
    }//GEN-LAST:event_btnExcluirActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        // TODO add your handling code here:
        if(this.tblAgencias.getSelectedRow() != -1){
            ControlAgencia c = new ControlAgencia();
            Agencia a = c.buscarAgencia((String) this.tblAgencias.getValueAt(this.tblAgencias.getSelectedRow(), 0));
            InclusaoAgencia ia = new InclusaoAgencia(this, a);
            ia.setVisible(true);
            
        }
    }//GEN-LAST:event_btnEditarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnFechar;
    private javax.swing.JButton btnNova;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblAgencias;
    // End of variables declaration//GEN-END:variables
}
